﻿//-----------------------------------------------------------------------
// <copyright file="SlimResponse.cs" company="billfellows.net">
//     I mention copyright so StyleCop is happy.
// </copyright>
//-----------------------------------------------------------------------
namespace ReputationMinder
{
    using System;
    using System.Linq;
    using System.Net;
    using System.Text;    

    /// <summary>
    /// Provide a simple class for tracking response text and hashing.
    /// </summary>
    public struct SlimResponse
    {
        /// <summary>
        /// Gets or sets the content (html)
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// Gets or sets the HTTP status code
        /// </summary>
        public HttpStatusCode StatusCode { get; set; }

        /// <summary>
        /// Gets or sets the content length
        /// </summary>
        public long ContentLength { get; set; }

        /// <summary>
        /// Gets an MD5 hash of the content. Useful for comparing contents
        /// </summary>
        public string ContentHash
        {
            get
            {
                // http://blogs.msdn.com/b/csharpfaq/archive/2006/10/09/how-do-i-calculate-a-md5-hash-from-a-string_3f00_.aspx
                System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(this.Content);
                byte[] hash = md5.ComputeHash(inputBytes);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                {
                    sb.Append(hash[i].ToString("X2"));
                }

                return sb.ToString();
            }
        }
    }
}
